<template>
  <div>
    <div>
      <el-form :inline="true">
        <el-form-item label="需求类型">
          <el-radio-group v-model="searchForm.status" @change="handleStatusChange">
            <el-radio-button :label="0">全部</el-radio-button>
            <el-radio-button :label="1">未处理</el-radio-button>
            <el-radio-button :label="2">已处理</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="筛选">
          <el-input v-model="searchForm.s" aria-placeholder="请输入需求内容搜索" placeholder="请输入需求内容搜索" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handSearch">筛选</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table v-loading="loading" border :data="tableData">
        <el-table-column label="需求类型" width="100" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.type === 1">讲座对接</span>
            <span v-if="scope.row.type === 2">研学实践</span>
            <span v-if="scope.row.type === 3">活动邀约</span>
            <span v-if="scope.row.type === 4">其他</span>
          </template>
        </el-table-column>
        <el-table-column label="需求内容" prop="content" />
        <el-table-column label="平台反馈" prop="reply" />
        <el-table-column label="反馈时间" width="100" prop="reply_at" align="center" />
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div>
        <el-pagination
          :current-page="paginator.page"
          :total="paginator.total"
          :page-size="paginator.limit"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getList, destroy } from '../../api/demand'
export default {
  data() {
    return {
      searchForm: {
        s: '',
        status: 0
      },
      tableData: [],
      loading: true,
      paginator: {
        total: 0,
        page: 1,
        limit: 10
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleSizeChange(size) {
      this.paginator.limit = size
      this.getList()
    },
    handleCurrentChange(page) {
      this.paginator.page = page
      this.getList()
    },
    handleDelete(row) {
      console.log(row)
      this.$confirm('此操作将删除该需求, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        destroy(row.id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '删除完成'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handSearch() {
      this.getList()
    },
    handleStatusChange(status) {
      console.log(status)
      this.searchForm.status = status
      this.getList()
    },
    getList() {
      this.loading = true
      getList({
        limit: this.paginator.limit,
        page: this.paginator.page,
        s: this.searchForm.s,
        status: this.searchForm.status
      }).then((res) => {
        console.log(res)
        this.tableData = res.data
        this.paginator.total = res.total
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

